<template>
  <div id="UserAdd">
    <el-card style="width: 98%; height: 55vh" shadow="always">
      <div
        class="category-top"
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          line-height: 50px;
          border-bottom: 2px solid rgb(244, 245, 250);
        "
      >
        <h3>个人中心</h3>
      </div>
      <div class="content">
        <ul class="ul">
          <li>管理员ID：{{ userInfo.id }}</li>
          <li>账号：{{ userInfo.account }}</li>
          <li>用户组：{{ userInfo.userGroup }}</li>
          <li>创建时间：{{ moment(userInfo.ctime).format('YYYY-MM-DD HH:mm:ss') }}</li>
          头像：<br />
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/users/avatar_upload"
            method="post"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <!-- <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon> -->
          </el-upload>
        </ul>
        <el-button style="margin-top: 10px" type="primary" @click="EditImg"> 修改头像</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { fetchUserInfo, fetchUserAvatarEdit } from '../api/users/index'
import moment from 'moment'
const userInfo = ref('')

import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'

const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = async (response, uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)

  const res = await fetchUserAvatarEdit({ imgUrl: response.imgUrl })
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
//修改图片
const EditImg = () => {
  const uploadElement = document.querySelector('.avatar-uploader .el-upload') as HTMLElement
  uploadElement.click()
}
//初始化加载页面信息
onMounted(async () => {
  const res = await fetchUserInfo()
  userInfo.value = res.accountInfo
  imageUrl.value = res.accountInfo.imgUrl
})
</script>

<style scoped>
.content {
  width: 100%;
  height: auto;
  .ul {
    width: 100%;
    height: auto;
    li {
      width: 100%;
      height: 50px;
      display: flex;
      align-items: center;
      border-bottom: 2px solid rgb(231, 233, 238);
      list-style: none;
    }
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
img {
  width: 178px;
  height: 178px;
  background-size: cover;
}
</style>
